package org.springblade.modules.mes.base.job;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.modules.mes.base.entity.LMSDetail;
import org.springblade.modules.mes.base.entity.PrdInfo;
import org.springblade.modules.mes.base.entity.ProducePlan;
import org.springblade.modules.mes.base.service.ILmsProductionOrderService;
import org.springblade.modules.mes.base.service.IProducePlanService;
import org.springblade.modules.mes.opcua.constant.OpcUaCache;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@AllArgsConstructor
@Slf4j
public class ProducePlanJobService {

	private final IProducePlanService producePlanService;
	private final ILmsProductionOrderService lmsProductionOrderService;

	@Scheduled(fixedDelay = 600 * 1000)
	public void publish() {
		Date now = new Date();
		Date begin = DateUtil.beginOfDay(now);
		Date end = DateUtil.offsetDay(begin, 1);
		//创建时间大于15分钟的处理，防止错误，给15分钟考虑几乎
		Date createTime = DateUtil.offsetMinute(now, -15);
		List<ProducePlan> dataList = producePlanService.list(new QueryWrapper<ProducePlan>().lambda()
			.eq(ProducePlan::getStatus, 0)
			.lt(ProducePlan::getProdTime, end)
			.lt(ProducePlan::getCreateTime, createTime)
			.ge(ProducePlan::getProdTime, begin));
		if (dataList == null || dataList.size() == 0) {
			return;
		}
		String nowDate = DateUtil.format(new Date(), "yyyyMMdd");
		Long base = 10000L;
		Long temp;
		LMSDetail order;

		for (ProducePlan data : dataList) {
			List<LMSDetail> orderList = new ArrayList<>();
			for (int i = 0; i < data.getCreateNumber(); i++) {
				temp = base + i + data.getBeginIdx();
				StringBuilder barCode = new StringBuilder();
				if (temp >= base * 2) {
					barCode.append(data.getCustomCode()).append("-").append(nowDate).append(temp);
				} else {
					barCode.append(data.getCustomCode()).append("-").append(nowDate).append(String.valueOf(temp).substring(1));
				}
				order = new LMSDetail();
				order.setId(IdUtil.getSnowflake(1, 2).nextId());
				order.setBarCode(barCode.toString());
				order.setSequence(i + data.getBeginIdx());
				order.setMatCode(data.getProdCode());
				order.setMatName(data.getProdName());
				order.setWorkClassCode(data.getWorkClassCode());
				order.setWorkClassName(data.getWorkClassName());
				order.setPlanCode(data.getPlanCode());
				order.setMatNum(1L);
				PrdInfo prd = OpcUaCache.getByProdCode(order.getMatCode());
				if (prd != null) {
					order.setLineCode(prd.getProductionLineCode());
					order.setCarType(prd.getMaterialType());
				}
				order.setCreateTime(new Date());
				order.setUpdateTime(new Date());
				order.setIsDeleted(0);
				order.setStatus(0);
				orderList.add(order);
			}
			try {
				//批量保存
				if (lmsProductionOrderService.saveBatch(orderList)) {
					//修改已下放
					UpdateWrapper<ProducePlan> wrapper = new UpdateWrapper<ProducePlan>();
					wrapper.set("status", 1);
					wrapper.eq("id", data.getId());
					producePlanService.update(wrapper);
				}
			} catch (Exception ex) {
				log.error("异常", ex);
			}
		}


	}
}
